package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.AuditStatus;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.common.enums.ZhiRuZhiChu;
import com.ruoyi.system.domain.CaiGouDan;
import com.ruoyi.system.domain.HuoPin;
import com.ruoyi.system.domain.RuKuDan;
import com.ruoyi.system.mapper.CaiGouDanMapper;
import com.ruoyi.system.mapper.HuoPinMapper;
import com.ruoyi.system.mapper.RuKuDanMapper;
import com.ruoyi.system.service.IChuKuDanDetailService;
import com.ruoyi.system.service.IRuKuDanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 入库单信息 服务实现类
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-20
 */
@Service
public class RuKuDanServiceImpl extends ServiceImpl<RuKuDanMapper, RuKuDan> implements IRuKuDanService {

    @Resource
    private HuoPinMapper huoPinMapper;

    @Resource
    private CaiGouDanMapper caiGouDanMapper;

    @Resource
    private IChuKuDanDetailService chuKuDanDetailService;

    @Override
    public List<RuKuDan> selectRuKuDanList(RuKuDan req) {
        return baseMapper.selectRuKuDanList(req);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean audit(RuKuDan auditInfo) {
        RuKuDan ruKuDan = baseMapper.selectById(auditInfo.getId());
        ruKuDan.setUpdateTime(new Date());
        if( AuditStatus.PASS.getCode().equals(auditInfo.getStatus())) {
            // 如果审核通过，同时修改货品的库存
            HuoPin huoPin = huoPinMapper.selectById(ruKuDan.getHuoPinId());

            BigDecimal kuCun = huoPin.getKuCun();
            kuCun = kuCun.add(ruKuDan.getRuKuCount());
            huoPin.setKuCun(kuCun);
            huoPinMapper.updateById(huoPin);

            // 如果是直入直出，直接生成出库单
            if(ZhiRuZhiChu.YES.getCode().equals(auditInfo.getZhiChu())){
                chuKuDanDetailService.createByRukuDan(ruKuDan);
            }
        }

        baseMapper.updateById(auditInfo);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean createByCaiGouDan(List<CaiGouDan> caiGouDans, String username) {

        String batchId = UUID.randomUUID().toString();

        for(CaiGouDan caiGouDan : caiGouDans) {
            RuKuDan ruKuDan = new RuKuDan();
            ruKuDan.setBatchId(batchId);
            ruKuDan.setHuoPinId(caiGouDan.getHuoPinId());
            ruKuDan.setDingDanId(caiGouDan.getDingDanId());
            ruKuDan.setDingDanDetailId(caiGouDan.getDingDanDetailId());
            ruKuDan.setRuKuCount(caiGouDan.getRealBuyCount());
            ruKuDan.setRuKuPrice(caiGouDan.getRealPrice());
            ruKuDan.setShengChanRiQi(caiGouDan.getShengChanRiQi());
            ruKuDan.setBaoZhiQi(caiGouDan.getBaoZhiQi());
            ruKuDan.setShengChanShang(caiGouDan.getShengChanShang());
            ruKuDan.setDelFlag(DelFlag.NOMAL.getCode());
            ruKuDan.setRuKuTime(LocalDateTime.now());
            ruKuDan.setCreateBy(username);
            ruKuDan.setCreateTime(new Date());

            RuKuDan old = selectByDingDanDetailId(caiGouDan.getDingDanDetailId());
            if(old == null){
                baseMapper.insert(ruKuDan);
            } else {
                ruKuDan.setId(old.getId());
                baseMapper.updateById(ruKuDan);
            }
        }

        return true;
    }

    @Override
    public RuKuDan selectByDingDanDetailId(Long dingDanDetailId) {
        LambdaQueryWrapper<RuKuDan> query = new LambdaQueryWrapper<>();
        query.eq(RuKuDan::getDingDanDetailId, dingDanDetailId);
        query.eq(RuKuDan::getDelFlag, DelFlag.NOMAL.getCode());
        return getOne(query, false);
    }

    @Override
    public RuKuDan queryAuditInfo(Long id) {
        return baseMapper.queryAuditInfo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveImportExcel(List<List<String>> list) {
        List<RuKuDan> ruKuDans = new ArrayList<>();
        String uuid = UUID.randomUUID().toString();
        for(List<String> item : list) {
            RuKuDan ruKuDan = new RuKuDan();
            ruKuDan.setBatchId(uuid);
            ruKuDan.setHuoPinId(Long.parseLong(item.get(5)));
            ruKuDan.setRuKuCount(new BigDecimal(item.get(11)));
            ruKuDan.setRuKuPrice(new BigDecimal(item.get(12)));
            ruKuDan.setShengChanRiQi(item.get(13));
            ruKuDan.setBaoZhiQi(item.get(14));
            ruKuDan.setShengChanShang(item.get(15));
            ruKuDan.setStatus(AuditStatus.INIT.getCode());
            ruKuDan.setCreateTime(new Date());
            ruKuDan.setDelFlag(DelFlag.NOMAL.getCode());
            baseMapper.insert(ruKuDan);
        }
    }
}
